
module EventEditor
  module Commands
    class SelfSwitch
      @@icon = Gdk::Pixbuf.new("icon/command/switch.xpm")
      def self.show(base, model, edit_command = [], options = {}, &func)
       @dialog = Gtk::Dialog.new("Self Switch",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @model = model
        box = Gtk::HBox.new
        @self_switch_combobox = ::Gadgets::Combobox.new($config.self_switches, [String, Integer])
        box.add(@self_switch_combobox)

        box.pack_start(@switch_input, false, false)
        box.pack_start(Gtk::Label.new("is"))
        @combobox = ::Gadgets::Combobox.new([['ON', 'on'], ['OFF', 'off']], [String, String])
        box.pack_start(@combobox, false, false)
        
        @dialog.vbox.add(box)
        @dialog.vbox.show_all
        
        self.load(edit_command)
        
        response = @dialog.run
        if response == Gtk::Dialog::RESPONSE_OK
          command = self.command
          func.call(command) 
        end
        @dialog.destroy
      end
      
      def self.load(edit_command)
        unless edit_command.empty?
          @self_switch_combobox.select(edit_command[2])
          @combobox.select(edit_command[2])
        end
      end
      
      def self.command
        return ['self_switch', @self_switch_combobox.selected_data, @combobox.selected_data]
      end
      
      def self.command_to_s(model, args, margin = "")
        arr = {'on' => 'ON', 'off' => 'OFF'}
        s = ""
        s += '<b>Self Switch:</b> '
        s += $config.self_switches[args[1]][0] + " = " + arr[args[2]]
        return s
      end
      
      def self.icon
        return @@icon
      end
    end
  end
end